<template>
  <q-page class="cc-admin row">

    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table
        flat
        color="primary"
        class="cross_table"
        separator="cell"
        :columns="columns"
        :data="list"
        row-key="id"
        :pagination.sync="pagination"
        :visible-columns="group"
        @request="query"
        :rows-per-page-options="[10,20,50,100]"
        :loading="loading"
        selection="multiple"
        :selected.sync="selected"
      >
        <template v-slot:top="table">
          <div class="row no-wrap full-width">
            <q-input
              clearable
              outlined
              dense
              placeholder="请输入关键字搜索"
              class="on-left"
              @input="query"
              debounce="500"
              v-model="key"
            >
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <div class="q-gutter-xs">
              <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />

              <q-btn-dropdown
                color="primary"
                label="自选列."
                icon="view_list"
                no-wrap
                v-show="$q.screen.gt.md"
              >
                <q-list>
                  <q-item tag="label" v-for="item in columns" :key="item.name">
                    <q-item-section avatar>
                      <q-checkbox v-model="group" :val="item.name" />
                    </q-item-section>
                    <q-item-section>
                      <q-item-label>{{item.label}}</q-item-label>
                    </q-item-section>
                  </q-item>
                </q-list>
              </q-btn-dropdown>
            </div>
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="primary" icon="edit" @click="editIncome(props.row)">
              <q-tooltip>编辑</q-tooltip>
            </q-btn>
            <btn-del label="应用管理" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{editType}}收入管理
          <q-btn  outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-6">
              <h5>
                <q-icon name="star" color="red" />客户：
              </h5>
              <q-select color="white-2" bg-color="green" filled v-model="form.client_id" :options="clientOptions" :rules="[requiredTest]" >
                <template v-slot:prepend>
                  <q-icon name="event" />
                </template>
              </q-select>

            </div>
            <div class="col-6">
              <h5>
                <q-icon name="star" color="red" />项目id：
              </h5>
              <q-select color="white-2" bg-color="green" filled v-model="form.project_id" :options="projectOptions" :rules="[requiredTest]" >
                <template v-slot:prepend>
                  <q-icon name="event" />
                </template>
              </q-select>

            </div>
            <div class="col-6">
              <h5>
                <q-icon name="star" color="red" />账期：
              </h5>

              <q-field outlined dense v-model="form.account_date">
                <template v-slot:control>{{form.account_date}}</template>
                <template v-slot:append>
                  <q-btn flat dense round color="primary" icon="today">
                    <q-popup-proxy>
                      <q-date v-model="form.account_date" mask="YYYY-MM-DD" />
                    </q-popup-proxy>
                  </q-btn>
                </template>
              </q-field>
            </div>
            <div class="col-6">
              <h5>编号：</h5>

              <q-input outlined disable v-model="form.income_code" type="text" />
            </div>
            <div class="col-6">
              <h5>
                <q-icon name="star" color="red" />金额：
              </h5>
              <q-input outlined  v-model="form.money" type="number" :rules="[requiredTest]" />
            </div>
            <div class="col-6">
              <h5>
                <q-icon name="star" color="red" />总金额：
              </h5>
              <q-input outlined  v-model="form.amount_money" type="number" :rules="[requiredTest]" />
            </div>
            <div class="col-6">
              <h5>部门名称：</h5>
              <q-select color="white-2" bg-color="green" filled v-model="form.department_id" :options="departmentOptions" :rules="[requiredTest]" >
                <template v-slot:prepend>
                  <q-icon name="event" />
                </template>
              </q-select>

            </div>
            <div class="col-6">
              <h5>联系人：</h5>
              <q-input outlined  v-model="form.contacter"   />
            </div>
            <div class="col-6">
              <h5>产品：</h5>
              <q-select color="white-2" bg-color="green" label="产品"  filled v-model="form.product" :options="productOptions" :rules="[requiredTest]" >
                <template v-slot:prepend>
                  <q-icon name="event" />
                </template>
              </q-select>
            </div>
            <div class="col-1">
              <h5>是否含税：</h5>
              <q-radio v-model="form.has_fee" val="0" label="否"  :rules="[requiredTest]" />
              <q-radio v-model="form.has_fee" val="1" label="是"  :rules="[requiredTest]" />

            </div>
            <div class="col-6">
              <h5>公司账户：</h5>
              <q-input outlined  v-model="form.company_account_id" :rules="[requiredTest]" />
            </div>
            <div class="col-6">
              <h5>备注：</h5>
              <q-input outlined  v-model="form.remark" type="textarea" />
            </div>
            <div class="col-6">
              <h5>审批：</h5>
              <q-input outlined  v-model="form.approve"  />
            </div>
            <div class="col-6">
              <h5>抄送：</h5>
              <q-input outlined  v-model="form.send_copy"  />
            </div>
          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-sm" color="primary" icon="mdi-check-bold" label="提交" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import confirm from 'components/confirm';
import { requiredTest } from 'boot/inputTest';
import catalogselect from 'components/catalogselect';
import { date } from 'quasar';
const today = new Date()
const { addToDate, subtractFromDate } = date
export default {
  name: 'MntApp',
  mixins: [IndexMixin],
  components: {
    confirm,
    catalogselect,
  },
  data() {
    return {
      form: {
        id: '',
        has_fee:0,
      },
      columns: [
        // {
        //   name: 'index',
        //   align: 'center',
        //   label: '序号',
        //   field: 'index',
        // },
        {
          name: 'client_name', align: 'left', label: '客户', field: 'client_name',
        },
        {
          name: 'project_name', align: 'left', label: '项目', field: 'project_name',
        },
        {
          name: 'account_date', align: 'left', label: '账期', field: 'account_date',
        },
        {
          name: 'income_code', align: 'left', label: '编号', field: 'income_code',
        },
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/goodslist',
        add: '/income/add',
        edit: '/income/edit',
        delete: '/index/delete',
        // delete: 'http://test4.geekwonderland.net/index/index/delete',
        deleteBatch: '/mnt/app/deleteBatch',
        exportXlsUrl: '/mnt/app/exportXls',
        importExcelUrl: '/mnt/app/importExcel',
      },
      model: '2016-10-24T10:40:14.674Z',
      dateMinimal: null,
      minMaxModel: today,
      min: subtractFromDate(today, {days: 5}),
      max: addToDate(today, {days: 4, month: 1, minutes: 10}),
      clientOptions: [],
      projectOptions: [],
      newCode: [],
      productOptions: ['产品一', '产品二'],
      departmentOptions: ['业务员', '财务部'],
    };
  },
  methods: {
    editIncome(p){
      console.log('edit income')
      console.log(p.id)
      if (this.editBefore(p)) {
        this.$axios.post('/income/getdetail',{ id: p.id} ).then((r) => {
          if(r.success = true){
            this.form = r.data
            this.form.client_id = r.data.client_data
            this.form.project_id = r.data.project_data
          }
        });
        this.isEdit = 1
        this.editType = '编辑';
        this.$refs.dialog.show();
      }
      console.log(this.form)
      this.editAfter();
    },
    requiredTest,
    addBefore() {
      this.form.catalogId = this.catalog;
      return true;
    },
    selectCatalog(n) {
      this.catalog = n;
      this.query();
    },
    getDictLabel,
    initDict() {
      // this.getClient()
      // this.getProject()

    },
    getClient(){
      this.clientOptions = []
      this.$axios.get('/client/getlist').then((r) => {
        //this.babyWord = r;
        if(r.success = true){
          for(let i = 0;  i < r.result.records.length; i++){
            let temp = {'label': r.result.records[i].name , 'value': r.result.records[i].id};
            this.clientOptions.push(temp)

          }
        }
      });
    },
    getProject(){
      this.projectOptions = []
      this.$axios.get('/project/getlist').then((r) => {
        //this.babyWord = r;
        if(r.success = true){
          for(let i = 0;  i < r.result.records.length; i++){
            let temp = {'label': r.result.records[i].project_name , 'value': r.result.records[i].id};
            this.projectOptions.push(temp)

          }
        }
      });
    },
    getCode(){
      this.$axios.get('/income/getcode').then((r) => {
        //this.babyWord = r;
        if(r.success = true){
          this.newCode = r.data
          this.form.income_code = this.newCode
          console.log(this.newCode)
        }
      });
    },
    reset(){
      this.form = {
        id: '',
      }
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus"></style>
